package com.qf.j2112.controller;

import com.qf.j2112.anno.JWTAnno;
import com.qf.j2112.po.TourArea;
import com.qf.j2112.service.TourAreaService;
import com.qf.j2112.utils.GoodItemsUtil;
import com.qf.j2112.utils.PageUtil;
import com.qf.j2112.utils.QiNiuUtil;
import com.qf.j2112.vo.ActionResultVO;
import com.qf.j2112.vo.ClientActionResultVo;
import com.qf.j2112.vo.GoodsItemsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/api")
@CrossOrigin
public class TourAreaController {

    @Resource
    private TourAreaService tourAreaService;

    //获取全部景区列表，包含下架的景区
    @JWTAnno(name = "管理员获取全部景区列表")
    @GetMapping("/areas")
    public ActionResultVO getAreaList(PageUtil pageUtil){
        ActionResultVO vo=null;
        List<TourArea> tourList = tourAreaService.findAll(pageUtil);
        int num=tourAreaService.countArea();
        if (!ObjectUtils.isEmpty(tourList)){
            vo=new ActionResultVO(200,"查询全部景区成功", tourList,num);
        }else {
            vo=new ActionResultVO(-200,"查询全部景区失败",null);
        }
        return vo;
    }

    //获取全部正常的景区列表，不包含下架的景区
    @JWTAnno(name = "用户端获取全部可用景区列表")
    @PostMapping("/goodItems")
    public ClientActionResultVo getUsefulAreaList(){
        ClientActionResultVo vo=null;
        List<TourArea> tourList = tourAreaService.findUsefulTourArea();
        if (!ObjectUtils.isEmpty(tourList)){
            List<GoodsItemsVo> goodsItemsVos = new ArrayList<>();
            for (TourArea tourArea : tourList) {
                if (tourArea.getStatus().equals("正常")){
                    GoodsItemsVo goodsItemsVo = GoodItemsUtil.itemsVo(tourArea);
                    goodsItemsVos.add(goodsItemsVo);
                }
            }
            vo=new ClientActionResultVo(200,"查询可用景区成功",goodsItemsVos);
        }else {
            vo=new ClientActionResultVo(-200,"查询可用景区失败",null);
        }
        return vo;
    }

    //根据景区id单查一个景区
    @JWTAnno(name = "查询一个景区的详细信息")
    @GetMapping("/getAreaInfo")
    public ActionResultVO getAreaDetail(int aid){
        ActionResultVO vo=null;
        TourArea tourArea = tourAreaService.findOneByAid(aid);
        if (!ObjectUtils.isEmpty(tourArea)){
            vo=new ActionResultVO(200,"查询单个景区成功",tourArea);
        }else {
            vo=new ActionResultVO(-200,"查询单个景区失败",null);
        }
        return vo;
    }

    //根据景区id单查一个景区
    @JWTAnno(name = "添加一个景区")
    @PostMapping ("/addArea")
    public ActionResultVO addArea(@RequestBody TourArea tourArea) throws IOException {
        log.info(tourArea.toString());
        tourArea.setRefreshTime(new Date());
        ActionResultVO vo=null;
        TourArea area = tourAreaService.addTourArea(tourArea);
        if (tourArea.getAid()!=0){
            vo=new ActionResultVO(200,"添加景区成功",area);
        }else {
            vo=new ActionResultVO(-200,"添加景区失败",null);
        }
        return vo;
    }

    @JWTAnno(name = "修改景区的图片")
    @PostMapping ("/addAreaPic")
    public ActionResultVO addAreapic( MultipartFile pic ,Integer areaId) throws IOException {
        ActionResultVO vo=null;
        InputStream is = pic.getInputStream();
        if (is != null) {
            log.info("我已获得上传的文件流");
            log.info("originName:" + pic.getOriginalFilename() + " name:" + pic.getName());
        }
        log.info(areaId.toString());
        String fileType=pic.getOriginalFilename().substring(pic.getOriginalFilename().lastIndexOf("."));
        log.info("上传的文件类型："+fileType);
        String uuidName=UUID.randomUUID().toString();
        log.info("文件名："+uuidName);
        String fileName=uuidName.replace("-","")+fileType;
        log.info("文件名2："+fileName);
        try {
            String url = QiNiuUtil.updateFile(pic, fileName);
            log.info("上传文件得到的url是："+url);
            TourArea area = tourAreaService.findOneByAid(areaId);
            if(area.getApic1()==null){
                area.setApic1(url);
            }else if(area.getApic2()==null){
                area.setApic2(url);
            }else {
                area.setApic3(url);
            }
            area.setRefreshTime(new Date());
            boolean b = tourAreaService.updateTourArea(area);
            if(b){
                vo=new ActionResultVO(200,"添加景区照片成功",url);
            }else{
                vo=new ActionResultVO(-200,"更新景区照片失败",url);
            }
        } catch (Exception e) {
            e.printStackTrace();
            vo=  new ActionResultVO(-200,"上传到七牛云异常",null);
        }
        return vo;
    }

    //修改景区信息
    @JWTAnno(name = "管理员修改一个景区")
    @PutMapping ("/updateArea")
    public ActionResultVO updateArea(@RequestBody TourArea tourArea){
        tourArea.setRefreshTime(new Date());
        ActionResultVO vo=null;
        if (tourAreaService.updateTourArea(tourArea)){
            vo=new ActionResultVO(200,"修改景区成功",null);
        }else {
            vo=new ActionResultVO(-200,"修改景区失败",null);
        }
        return vo;
    }

    //删除一个景区
    @JWTAnno(name = "管理员删除一个景区")
    @DeleteMapping ("/deleteArea")
    public ActionResultVO deleteteArea(int aid){
        ActionResultVO vo=null;
        if (tourAreaService.deleteTourArea(aid)){
            vo=new ActionResultVO(200,"删除景区成功",null);
        }else {
            vo=new ActionResultVO(-200,"删除景区失败",null);
        }
        return vo;
    }

    //管理员禁用一个景区
    @JWTAnno(name = "管理员禁用一个景区")
    @DeleteMapping ("/banArea")
    public ActionResultVO banArea(int aid,String status){
        ActionResultVO vo=null;
        if (tourAreaService.banTourArea(aid,status)){
            vo=new ActionResultVO(200,"禁用景区成功",null);
        }else {
            vo=new ActionResultVO(-200,"禁用景区失败",null);
        }
        return vo;
    }

    @PutMapping ("/changeAreaState")
    @JWTAnno(name = "修改一个景区的状态")
    public ActionResultVO changeAreaState(@RequestBody TourArea tourArea){
        log.info("景区：",tourArea);
        tourArea.setRefreshTime(new Date());
        ActionResultVO vo=null;
        if(tourAreaService.updateTourArea(tourArea)){
            vo=new ActionResultVO(200,"修改景区状态成功",null);
        }else {
            vo=new ActionResultVO(-200,"修改景区状态失败",null);
        }
        return vo;
    }
}
